<template>
  <el-dialog
    title="新增角色组"
    :visible.sync="addRoleGroupDialogVisible"
    :before-close="cancel"
    :close-on-click-modal="false"
    class="vc-dialog-form"
    width="480px">
    <el-form ref="addForm" :model="addForm" :rules="rules" label-width="100px" size="small">

        <input type="hidden" v-model="addForm.pid" maxlength="10" readonly></input>


      <el-form-item label="角色组名称" prop="name">
        <el-input v-model="addForm.name" maxlength="10" clearable></el-input>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button type="primary" size="small" :disabled="disabled" @click="save">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  name: 'addRoleGroup',

  props: ['addRoleGroupDialogVisible', 'pid'],
  data () {
    return {
      addForm: {
        pid: '',
        name: ''

      },
      rules: {
        pid: [
          { required: true, message: '请输入父菜单ID', trigger: 'blur' },
          { pattern: /^\S+(\s+\S+)*$/, message: '不能以空格开头或者结尾', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { pattern: /^\S+(\s+\S+)*$/, message: '不能以空格开头或者结尾', trigger: 'blur' }
        ]
      },
      disabled: false,
      readonly: true

    }
  },
  watch: {
    addRoleGroupDialogVisible (val) {
      if (!val) return
      this.$nextTick(() => {
        this.$refs.addForm && this.$refs.addForm.resetFields()
        this.addForm.pid = this.pid
        setTimeout(() => { this.$refs.addForm.clearValidate() }, 0)
      })
    }
  },
  methods: {
    /**
     * 保存
     */
    save () {
      if (this.disabled) {
        return
      }
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.disabled = true
        this.$api.addRoleGroup({
          data: this.addForm
        }).then((res) => {
          this.disabled = false
          if (!res.success) {
            return;
          }
          this.$emit('close', res.data || {})
          this.$notifyBox('新增成功', 'success')
        }).catch((err) => {
          this.disabled = false
        })
      })
    },
    /**
     * 取消
     */
    cancel () {
      this.$emit('close', false)
    }

  }
}
</script>
